<?php

namespace App\Http\Controllers\Manage;


use App\Manage\OrderManage;
use App\Models\Event;
use App\Models\Order;
use App\Models\User;

class OrderController extends BaseController
{

    // 用户
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Order';
    }


    // 列表
    // 添加
    // 修改
    // 支付
    // 删除

    // 支付（后台不能支付）
    public function onPay()
    {
        $manage = new OrderManage();
        $result = $manage->onPay();
        if($result === false){
            return $this->error($manage->getError());
        }

        return $this->success([], '支付成功！');
    }

    // 退款审核
    public function onVerify()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $result = $manage->onVerify();
        if($result === false){
            return $this->error($manage->getError());
        }

        return $this->success();
    }

    // 确认退款
    public function confirmRefund()
    {
        $manage = new OrderManage();
        $result = $manage->confirmRefund();
        if($result === false){
            return $this->error($manage->getError());
        }

        return $this->success([], '确认退款成功！');
    }

    // 列表数据展示
    protected function listTable($lists)
    {
        $get_refund = $this->optLogic()->getData('get_refund', null, 'intval');
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 获取用户信息
        $uids  = array_column($lists, 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 活动信息
        $eids   = array_column($lists, 'event_id');
        $events = (new Event())->isCache(true)->getListInIds($eids);

        // 展示列表表格
        $listHtml = $this->adminList();
        $model    = new Order();
        $verifys  = $model->getVerify(false);
        $refunds  = [1 => '已经申请', 0 => '未申请'];
        foreach($lists as $key => $item){
            // 数据
            $_id = $item['id'];
            //$status   = $item['status'] == 1 ? '正常' : '禁用';
            //$statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = '';
            if($get_refund && ($item['verify'] == 0)){
                // 退款订单需要审核
                $operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1], '审核通过', 1, 'on-verify');
                $operate .= $listHtml->operate_status(['id' => $_id, 'value' => 2], '审核不通过', 2, 'on-verify');
            }
            $operate .= $listHtml->operate_list(['order_id' => $_id], '订单用户', '/order-user/lists');
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            //$operate .= $listHtml->operate_edit(['id' => $_id], '编辑');
            //$operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该订单吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['id']);
            $tr .= $listHtml->list_td($users[$item['uid']]['nickname'] ?? '-');
            $tr .= $listHtml->list_td($events[$item['event_id']]['event_name'] ?? '-');
            $tr .= $listHtml->list_td($item['order_no']);
            $tr .= $listHtml->list_td($item['number']);
            //$tr .= $listHtml->list_td($item['total']);
            $tr .= $listHtml->list_td($item['money']);
            $tr .= $listHtml->list_date($item['paytime'], 'datetime');
            //$tr .= $listHtml->list_td(date('Y-m-d H:i', $item['paytime']));
            if($get_refund){
                // 退款订单需要审核
                $tr .= $listHtml->list_td($verifys[$item['verify']] ?? '-');
            } else{
                $tr .= $listHtml->list_td($refunds[$item['is_refund']] ?? '-');
            }

            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/manage/order/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加订单</a> ';
        //$this->assign('top_button', $top_button);
        $this->assign('top_button', '');
    }

    // 列表字段
    protected function list_fields()
    {
        // 'status' => '状态', 'total' => '总价', 'verify' => '退款审核状态（0-未审核 1-审核通过 2-审核未通过）',
        $get_refund = $this->optLogic()->getData('get_refund', null, 'intval');
        $fields     = ['id' => 'ID', 'uid' => '用户ID', 'event_id' => '活动ID', 'order_no' => '订单号', 'number' => '报名人数', 'money' => '支付金额', 'paytime' => '支付时间', 'is_refund' => '退款状态', 'verify' => '退款审核', 'ctime' => '创建时间', 'operate' => '操作',];
        if($get_refund){
            // 退款订单字段
            unset($fields['is_refund']);
        } else{
            unset($fields['verify']);
        }

        return $fields;
    }

    // 列表搜索
    protected function list_search()
    {
        $model   = new Order();
        $refunds = [1 => '已经申请', 0 => '未申请'];
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();


        $uid        = $form->ajaxSingleFromList('uid', '用户id', 'user', 'id', 'username');
        $event_id   = $form->ajaxSingleFromList('event_id', '活动id', 'event', 'id', 'event_name');
        $order_no   = $form->formInput('order_no', $request['order_no'] ?? null, '订单编号', '180px');
        $is_refund  = $form->formSelect('is_refund', $request['is_refund'] ?? null, $refunds);
        $start_time = $form->formDateTime('start_time', isset($request['start_time']) ? date("Y-m-d H:i:s", $request['start_time']) : null, '开始时间', 'datetime');
        $end_time   = $form->formDateTime('end_time', isset($request['end_time']) ? date("Y-m-d H:i:s", $request['start_time']) : null, '结束时间时间', 'datetime');
        $search     = ['is_refund' => $is_refund, 'uid' => $uid, 'event_id' => $event_id, 'order_no' => $order_no, 'start_time' => $start_time, 'end_time' => $end_time];
        $this->assign('search', $search);

        $searchHide = ['get_refund'];
        $this->assign('searchHide', ['get_refund']);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        $model   = new Order();
        $refunds = [1 => '已经申请', 0 => '未申请'];

        // 活动
        $eInfo = (new Event())->isCache(true)->getOneById($info['event_id'] ?? 0);
        $uInfo = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);

        // 用户名
        //$status      = $form->formRadio('status', $info['status'] ?? 1, $statuss);
        //$uid      = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        //$uid      = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');


        $uid       = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $event_id  = $form->ajaxSingleFromList('event_id', $eInfo, 'Event', 'id', 'event_name');
        $order_no  = $form->formInput('order_no', $info['order_no'] ?? '', '订单号');
        $number    = $form->formNumber('number', $info['number'] ?? '', '人数');
        $paytime   = $form->formDateTime('paytime', $info['paytime'] ?? '', '支付时间', 'datetime');
        $is_refund = $form->formRadio('is_refund', $info['is_refund'] ?? 1, $refunds);
        $_id       = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('用户', $uid);
        $html .= $form->makeItem('活动', $event_id);
        $html .= $form->makeItem('订单号', $order_no);
        $html .= $form->makeItem('人数', $number);
        $html .= $form->makeItem('支付时间', $paytime);
        $html .= $form->makeItem('退款状态', $is_refund);
        $html .= $_id;

        return $html;
    }

}


